package org.ls.petclinic.web;

import com.google.gson.Gson;
import org.ls.petclinic.dao.UserDao;
import org.ls.petclinic.dao.impl.UserDaoImpl;
import org.ls.petclinic.pojo.User;
import org.ls.petclinic.untils.JdbcUtils;
import org.ls.petclinic.untils.ResultJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description
 * @Author 浪矢先生
 * @Create 2022-06-09 23:00
 */
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends BaseServlet{
    private UserDao userDao=new UserDaoImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = userDao.getUserByUsername(req.getParameter("username"));
        JdbcUtils.commitAndClose();
        if (user!=null){
            if (user.getPassword().equals(req.getParameter("password"))){
                req.getSession().setAttribute("user", user);
                resp.sendRedirect(req.getContextPath()+"/jsp/adminIndex.jsp");
                return;
            }
        }
            //失败记录错误信息,返回登录页面
            req.setAttribute("msg", "用户名或密码错误!");
            req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);

    }
}
